import {
  BadRequestException,
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
  UseGuards,
} from '@nestjs/common';
import { Roles, RolesGuard } from '@/auth/guard/roles.guard';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiParam,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { CategoryService } from '@/modules/category/category.service';
import { JwtAuthGuard } from '@/auth/guard/jwt-auth.guard';
import { CategoryEntity } from '@/entity/category.entity';
import { UpdateCategoryDto } from '@/modules/category/dto/update-category.dto';
import { CreateCategoryDto } from '@/modules/category/dto/create-category.dto';

@ApiTags('菜品类别管理')
@Controller('category')
@ApiBearerAuth()
export class CategoryController {
  constructor(private readonly categoryService: CategoryService) {}

  @ApiOperation({ summary: '查询所有菜品类别' })
  @Get()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('admin')
  async findAll(@Query('page') page = 1, @Query('limit') limit = 10) {
    if (isNaN(Number(page)) || isNaN(Number(limit))) {
      throw new BadRequestException('page or limit must be number');
    }
    if (page < 1) page = 1;
    if (limit < 1) limit = 10;
    return this.categoryService.findAll(page, limit);
  }

  @ApiOperation({ summary: '查询指定菜品类别' })
  @ApiParam({ name: 'id', description: '菜品类别ID' })
  @ApiResponse({
    status: 200,
    description: '返回指定菜品类别信息',
    type: CategoryEntity,
  })
  @Get(':id')
  async findOne(@Param('id') id: number): Promise<CategoryEntity> {
    return this.categoryService.findOne(id);
  }

  @ApiOperation({ summary: '创建菜品类别' })
  @Post()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('admin')
  async create(
    @Body() createCategoryDto: CreateCategoryDto,
  ): Promise<CategoryEntity> {
    return this.categoryService.create(createCategoryDto);
  }

  @ApiOperation({ summary: '更新菜品类别信息' })
  @ApiParam({ name: 'id', description: '菜品类别ID' })
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('admin')
  @ApiResponse({
    status: 200,
    description: '成功更新菜品类别信息',
    type: CategoryEntity,
  })
  @Put(':id')
  async update(
    @Param('id') id: number,
    @Body() updateCategoryDto: UpdateCategoryDto,
  ): Promise<CategoryEntity> {
    return this.categoryService.update(id, updateCategoryDto);
  }

  @ApiOperation({ summary: '删除菜品类别' })
  @ApiParam({ name: 'id', description: '菜品类别ID' })
  @ApiResponse({
    status: 200,
    description: '成功删除菜品类别',
    type: CategoryEntity,
  })
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('admin')
  @Delete(':id')
  async delete(@Param('id') id: number) {
    return this.categoryService.delete(id);
  }

  @ApiOperation({ summary: '查询指定餐厅 ID 下的菜品类别列表' })
  @ApiResponse({ status: 200, description: '成功', type: [CategoryEntity] })
  @Get('/restaurant/:restaurantId')
  async findByRestaurantId(
    @Param('restaurantId') restaurantId: number,
    @Query('page') page = 1,
    @Query('limit') limit = 10,
  ) {
    if (isNaN(Number(page)) || isNaN(Number(limit))) {
      throw new BadRequestException('page or limit must be number');
    }
    if (page < 1) page = 1;
    if (limit < 1) limit = 10;
    return this.categoryService.findByRestaurantId(restaurantId, page, limit);
  }
}
